﻿using UnityEngine;
using System.Collections;

namespace UI
{
    public class LabelNumRoll : MonoBehaviour
    {
        float startNum;
        float endNum;
        bool startRoll = false;
        float valuePer = 1;
        //float timer = 0;
        string frontText = "";
        string tailText = "";
        bool directionUp = true;
        string m_form = "{0:f0}";

        float speed = 0;
        float delta = 0;

        public void SetRoll(float start, float end)
        {
            startNum = start;
            endNum = end;
            startRoll = true;
        }

        public void SetForm(float value,string front,string tail, bool direct, string form, float sp)
        {
            m_form = form;
            directionUp = direct;
            frontText = front;
            tailText = tail;
            valuePer = value;
            speed = sp;
        }

        public void StopRoll()
        {
            startRoll = false;
        }

        // Update is called once per frame
        void Update()
        {
            if(startRoll)
            {
                if(speed > 0)
                {
                    delta += Time.deltaTime;
                    if (delta < speed)
                        return;
                    delta = 0;
                }
                if(directionUp)
                {
                    //timer += Time.deltaTime;
                    if (startNum < endNum)
                    {
                        startNum += valuePer;
                        if (startNum > endNum)
                            startNum = endNum;
                        GetComponent<UILabel>().text = frontText + string.Format(m_form,startNum) + tailText;
                    }
                    else
                    {
                        startRoll = false;
                    }
                }
                else
                {
                    if (startNum > endNum)
                    {
                        startNum -= valuePer;
                        if (startNum < 0)
                            startNum = 0;
                        GetComponent<UILabel>().text = frontText + string.Format(m_form, startNum) + tailText;
                    }
                    else
                    {
                        startRoll = false;
                    }
                }
            }
        }
    }
}

